<?php

namespace search\controllers\bigdata;

use business\elasticsearch\services\BigDataContactService;

class ContactController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     * @api {get} /bigdata/contact/get-list 01、大数据联系人主档案
     *
     * @apiVersion 1.0.0
     * @apiGroup ContactController
     *
     * @apiDescription 01、联系人主档案
     *
     * @apiParam {String} keyword                                            搜索词
     * @apiParam {String} province                                           省份
     * @apiParam {String} city                                               城市
     * @apiParam {String} page                                               页码
     * @apiParam {String} pageSize                                           分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     *
     */
    public function actionRecommend()
    {
        $id = $this->getParam('id');//支持array,用,分隔
        $companyName = $this->getParam('company_name');//支持array,用,分隔
        $organizingCode = $this->getParam('organizing_code');//支持array,用,分隔
        $currentCompanyName = $this->getParam('current_company_name');//string
        $currentOrganizingCode = $this->getParam('current_organizing_code');//string
        $subscriptProvinceCode = $this->getParam('subscript_province_code');
        $subscriptCityCode = $this->getParam('subscript_city_code');
        $isDeveloper = $this->getParam('is_developer');
        $isSupplier = $this->getParam('is_supplier');
        $bidKeyword = $this->getParam('bid_keyword');
        $supplierKeyword = $this->getParam('supplier_keyword');
        $searchKeyword = $this->getParam('search_keyword');
        $phoneType = $this->getParam('phone_type');
        $bidOwnerMobile = $this->getParam('bid_owner_mobile');
        $provinceCode = $this->getParam('province_code');//省份code，用于省份城市单选
        $cityCode = $this->getParam('city_code');//城市code，用于省份城市单选
        $areaCode = $this->getParam('area_code');//区域code，用于省份城市多选，省份code和城市code放在一起
        $page = $this->getParam('page');
        $pageSize = $this->getParam('page_size');
        $scoreWeight = $this->getParam('score_weight');//得分权重方式，3套：developer_contact、supplier_contact、cooperation_contact
        $page = intval($page) > 0 ? intval($page) : 1;
        $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;
        if (!empty($province)) {
            $province = explode(",", $province);
        } else {
            $province = [];
        }
        $subscriptProvinceCode = array_filter(explode(",", $subscriptProvinceCode));
        $subscriptCityCode = array_filter(explode(",", $subscriptCityCode));
        $provinceCode = array_filter(explode(",", $provinceCode));
        $cityCode = array_filter(explode(",", $cityCode));
        $areaCode = array_filter(explode(",", $areaCode));
        $searchKeyword = array_filter(explode(",", $searchKeyword));
        $companyName = array_filter(explode(",", $companyName));
        $organizingCode = array_filter(explode(",", $organizingCode));
        $bidKeyword = array_filter(explode(",", $bidKeyword));
        $supplierKeyword = array_filter(explode(",", $supplierKeyword));
        $phoneType = array_filter(explode(",", $phoneType));
        $id = array_filter(explode(",", $id));
        try {
            $data = (new BigDataContactService())->getRecommend(
                $id,
                $companyName,
                $organizingCode,
                $currentCompanyName,
                $currentOrganizingCode,
                $subscriptProvinceCode,
                $subscriptCityCode,
                $isDeveloper,
                $isSupplier,
                $bidKeyword,
                $supplierKeyword,
                $searchKeyword,
                $phoneType,
                $bidOwnerMobile,
                $provinceCode,
                $cityCode,
                $areaCode,
                $scoreWeight,
                $page,
                $pageSize
            );

            return $this->success($data);
        } catch (\Exception $exception) {
            \Yii::error('contact recommend:' . $exception->getTrace());
            return $this->error(-1, $exception->getMessage());
        }
    }


    /**
     * @api {get} /bigdata/saas-supplier/guess-names 02、合作单位，多工程单位
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataSaasSupplier
     *
     * @apiDescription 02、工程单位联想
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.titleHL     搜索词高亮开工项目名
     * @apiSuccess {String}  data.list.title       开工项目
     *
     *
     */
    public function actionCooperation()
    {
        $companyName = $this->getParam('company_name');//array
        $organizingCode = $this->getParam('organizing_code');//array
        $currentCompanyName = $this->getParam('current_company_name');//string
        $currentOrganizingCode = $this->getParam('current_organizing_code');//string
        $subscriptProvinceCode = $this->getParam('subscript_province_code');
        $subscriptCityCode = $this->getParam('subscript_city_code');
        $isDeveloper = $this->getParam('is_developer');
        $isSupplier = $this->getParam('is_supplier');
        $bidKeyword = $this->getParam('bid_keyword');
        $supplierKeyword = $this->getParam('supplier_keyword');
        $searchKeyword = $this->getParam('search_keyword');
        $phoneType = $this->getParam('phone_type');
        $bidOwnerMobile = $this->getParam('bid_owner_mobile');
        $provinceCode = $this->getParam('province_code');
        $cityCode = $this->getParam('city_code');
        $page = $this->getParam('page');
        $pageSize = $this->getParam('page_size');
        $page = intval($page) > 0 ? intval($page) : 1;
        $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;
        if (empty($companyName)) {
            return $this->error('-1', 'company_name不能为空');
        }
        $subscriptProvinceCode = array_filter(explode(",", $subscriptProvinceCode));
        $subscriptCityCode = array_filter(explode(",", $subscriptCityCode));
        $companyName = array_filter(explode(",", $companyName));
        $organizingCode = array_filter(explode(",", $organizingCode));
        $supplierKeyword = array_filter(explode(",", $supplierKeyword));
        $searchKeyword = array_filter(explode(",", $searchKeyword));

        try {
            $data = (new BigDataContactService())->getCooperation(
                $companyName,
                $organizingCode,
                $currentCompanyName,
                $currentOrganizingCode,
                $subscriptProvinceCode,
                $subscriptCityCode,
                $isDeveloper,
                $isSupplier,
                $bidKeyword,
                $supplierKeyword,
                $searchKeyword,
                $phoneType,
                $bidOwnerMobile,
                $provinceCode,
                $cityCode,
                $page,
                $pageSize
            );

            return $this->success($data);
        } catch (\Exception $exception) {
            \Yii::error('contact cooperation:' . $exception->getTrace());
            return $this->error(-1, $exception->getMessage());
        }
    }

    /**
     * @api {get} /bigdata/saas-supplier/guess-names 02、合作单位，多工程单位
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataSaasSupplier
     *
     * @apiDescription 02、工程单位联想
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.titleHL     搜索词高亮开工项目名
     * @apiSuccess {String}  data.list.title       开工项目
     *
     *
     */
    public function actionCooperationList()
    {
        $companyList = $this->getParam('company_list');//array
        $currentCompanyName = $this->getParam('current_company_name');//string
        $currentOrganizingCode = $this->getParam('current_organizing_code');//string
        $subscriptProvinceCode = $this->getParam('subscript_province_code');
        $subscriptCityCode = $this->getParam('subscript_city_code');
        $isDeveloper = $this->getParam('is_developer');
        $isSupplier = $this->getParam('is_supplier');
        $bidKeyword = $this->getParam('bid_keyword');
        $supplierKeyword = $this->getParam('supplier_keyword');
        $searchKeyword = $this->getParam('search_keyword');
        $phoneType = $this->getParam('phone_type');
        $bidOwnerMobile = $this->getParam('bid_owner_mobile');
        $provinceCode = $this->getParam('province_code');
        $cityCode = $this->getParam('city_code');
        $page = $this->getParam('page');
        $pageSize = $this->getParam('page_size');
        $page = intval($page) > 0 ? intval($page) : 1;
        $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;
        if (count($companyList) == 0) {
            return $this->error('-1', 'company_list不能为空');
        }
        $subscriptProvinceCode = array_filter(explode(",", $subscriptProvinceCode));
        $subscriptCityCode = array_filter(explode(",", $subscriptCityCode));
        $supplierKeyword = array_filter(explode(",", $supplierKeyword));
        $searchKeyword = array_filter(explode(",", $searchKeyword));

        try {
            $data = (new BigDataContactService())->getCooperationNewFast(
                [],
                $companyList,
                $currentCompanyName,
                $currentOrganizingCode,
                $subscriptProvinceCode,
                $subscriptCityCode,
                $isDeveloper,
                $isSupplier,
                $bidKeyword,
                $supplierKeyword,
                $searchKeyword,
                $phoneType,
                $bidOwnerMobile,
                $provinceCode,
                $cityCode,
                $page,
                $pageSize
            );

            return $this->success($data);
        } catch (\Exception $exception) {
            var_dump($exception->getMessage());
            die;
            \Yii::error('contact cooperation-list:' . $exception->getTrace());
            return $this->error(-1, $exception->getMessage());
        }
    }

    // 省市雷达的列表，以公司分组获取联系人
    public function actionRadarContact()
    {
        $companyName = $this->getParam('company_name') ?: [];
        $organizingCode = $this->getParam('organizing_code') ?: [];
        $subscriptAreaCode = $this->getParam('subscript_area_code') ?: [];
        $bidKeyword = $this->getParam('bid_keyword') ?: [];
        $isDeveloper = $this->getParam('is_developer');
        $isSupplier = $this->getParam('is_supplier');

        if (empty($companyName) && empty($organizingCode)) {
            return $this->error('-1', '公司名不能为空');
        }

        try {
            $data = (new BigDataContactService())->getRadarContactList(
                $companyName,
                $organizingCode,
                $subscriptAreaCode,
                $bidKeyword,
                $isDeveloper,
                $isSupplier
            );

            return $this->success($data);
        } catch (\Exception $exception) {
            \Yii::error('contact actionRadarContact:' . $exception->getTrace());
            return $this->error(-1, $exception->getMessage());
        }
    }
}